package cn.iocoder.yudao.module.system.convert.wishlist;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.wishlist.vo.UserWishListCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.wishlist.vo.UserWishListFromMajorReqVO;
import cn.iocoder.yudao.module.system.controller.admin.wishlist.vo.UserWishListRespVO;
import cn.iocoder.yudao.module.system.controller.admin.wishlist.vo.UserWishListUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.wishlist.vo.UserWishListWithScoreRespVO;
import cn.iocoder.yudao.module.system.controller.admin.gugu.gugu.MajorAdmissionInfo;
import cn.iocoder.yudao.module.system.dal.dataobject.wishlist.UserWishListDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.UserScoresDO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户志愿表 Convert
 *
 * @author 芋道源码
 */
@Mapper
public interface UserWishListConvert {

    UserWishListConvert INSTANCE = Mappers.getMapper(UserWishListConvert.class);

    UserWishListDO convert(UserWishListCreateReqVO bean);

    UserWishListDO convert(UserWishListUpdateReqVO bean);

    @Mapping(source = "schoolUUID", target = "schoolUuid")
    UserWishListCreateReqVO convertFromMajor(UserWishListFromMajorReqVO bean);

    UserWishListRespVO convert(UserWishListDO bean);

    List<UserWishListRespVO> convertList(List<UserWishListDO> list);

    PageResult<UserWishListRespVO> convertPage(PageResult<UserWishListDO> page);

    /**
     * 转换志愿表和成绩信息为包含成绩的响应VO
     */
    @Mapping(source = "wishList.id", target = "id")
    @Mapping(source = "wishList.userId", target = "userId")
    @Mapping(source = "wishList.userScoreId", target = "userScoreId")
    @Mapping(source = "wishList.provinceName", target = "provinceName")
    @Mapping(source = "wishList.schoolUuid", target = "schoolUuid")
    @Mapping(source = "wishList.schoolName", target = "schoolName")
    @Mapping(source = "wishList.majorName", target = "majorName")
    @Mapping(source = "wishList.majorCode", target = "majorCode")
    @Mapping(source = "wishList.year", target = "year")
    @Mapping(source = "wishList.highScore", target = "highScore")
    @Mapping(source = "wishList.averageScore", target = "averageScore")
    @Mapping(source = "wishList.lowestScore", target = "lowestScore")
    @Mapping(source = "wishList.lowestSection", target = "lowestSection")
    @Mapping(source = "wishList.batchName", target = "batchName")
    @Mapping(source = "wishList.typeName", target = "typeName")
    @Mapping(source = "wishList.proScore", target = "proScore")
    @Mapping(source = "wishList.subjectSelection", target = "subjectSelection")
    @Mapping(source = "wishList.majorStandardCode", target = "majorStandardCode")
    @Mapping(source = "wishList.wishType", target = "wishType")
    @Mapping(source = "wishList.wishOrder", target = "wishOrder")
    @Mapping(source = "wishList.remark", target = "remark")
    @Mapping(source = "wishList.enrollmentPlan", target = "enrollmentPlan")
    @Mapping(source = "wishList.createTime", target = "createTime")
    @Mapping(source = "wishList.updateTime", target = "updateTime")
    @Mapping(source = "wishList.userProvince", target = "userProvince")
    @Mapping(source = "wishList.userTotalScore", target = "userTotalScore")
    @Mapping(source = "wishList.userSubjectSelection", target = "userSubjectSelection")
    @Mapping(source = "wishList.userYear", target = "userYear")
    @Mapping(source = "userScore.province", target = "scoreProvince")
    @Mapping(source = "userScore.grade", target = "scoreGrade")
    @Mapping(source = "userScore.firstSubject", target = "scoreFirstSubject")
    @Mapping(source = "userScore.secondSubject", target = "scoreSecondSubject")
    @Mapping(source = "userScore.score", target = "scoreTotal")
    @Mapping(source = "userScore.ranking", target = "scoreRanking")
    @Mapping(source = "userScore.rankingRange", target = "scoreRankingRange")
    @Mapping(source = "userScore.typeName", target = "scoreTypeName")
    @Mapping(source = "userScore.minScore", target = "scoreMinScore")
    @Mapping(source = "userScore.maxScore", target = "scoreMaxScore")
    @Mapping(source = "userScore.examYear", target = "scoreExamYear")
    @Mapping(source = "userScore.isCurrent", target = "scoreIsCurrent")
    @Mapping(source = "userScore.remark", target = "scoreRemark")
    UserWishListWithScoreRespVO convertWithScore(UserWishListDO wishList, UserScoresDO userScore);

    /**
     * 转换志愿表列表和成绩信息列表为包含成绩的响应VO列表
     */
    default List<UserWishListWithScoreRespVO> convertWithScoreList(List<UserWishListDO> wishList, List<UserScoresDO> userScores) {
        if (wishList == null || userScores == null) {
            return new ArrayList<>();
        }
        
        // 创建成绩ID到成绩对象的映射
        Map<Long, UserScoresDO> scoreMap = userScores.stream()
            .collect(Collectors.toMap(UserScoresDO::getId, score -> score));
        
        // 转换每个志愿记录
        return wishList.stream()
            .map(wish -> {
                UserScoresDO userScore = scoreMap.get(wish.getUserScoreId());
                return convertWithScore(wish, userScore);
            })
            .collect(Collectors.toList());
    }

}
